package com.chuangshu.chuangshuteam.controller;

import com.chuangshu.chuangshuteam.config.BaseContext;
import com.chuangshu.chuangshuteam.dto.Result;
import com.chuangshu.chuangshuteam.entity.AwardRecords;
import com.chuangshu.chuangshuteam.entity.Competitions;
import com.chuangshu.chuangshuteam.mapper.CompetitionMapper;
import com.chuangshu.chuangshuteam.service.AwardRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/award-records")
public class AwardRecordsController {

    @Autowired
    private AwardRecordsService awardRecordsService;
    @Autowired
    CompetitionMapper competitionMapper;

    /**
     * 查询所有获奖记录
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        List<AwardRecords> list = awardRecordsService.list();
        return Result.ok(list);
    }

    /**
     * 根据ID查询获奖记录
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        AwardRecords awardRecords = awardRecordsService.getById(id);
        return Result.ok(awardRecords);
    }

    /**
     * 添加获奖记录
     * @param awardRecords
     * @return
     */
    @PostMapping
    public Result save(@RequestBody AwardRecords awardRecords) {
        if (awardRecords == null) {
            return Result.fail("参数错误");
        }
        Long competitionId = awardRecords.getCompetitionId();
        if (competitionId == null||awardRecords.getCompetitionNameManual().isEmpty()) {
            return Result.fail("参数错误");
        }
        Competitions competitions = competitionMapper.selectById(competitionId);
        if (competitions == null||competitions.getName().isEmpty()) {
            return Result.fail("参数错误");
        }
        awardRecords.setCompetitionNameManual(competitions.getName());
        awardRecordsService.save(awardRecords);
        return Result.ok("添加成功");
    }

    /**
     * 更新获奖记录
     * @param awardRecords
     * @return
     */
    @PutMapping
    public Result update(@RequestBody AwardRecords awardRecords) {
        awardRecordsService.updateById(awardRecords);
        return Result.ok("更新成功");
    }

    /**
     * 删除获奖记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        awardRecordsService.removeById(id);
        return Result.ok("删除成功");
    }

    /**
     * 根据用户ID查询获奖记录
     * @param userId
     * @return
     */
    @GetMapping("/user/{userId}")
    public Result getByUserId(@PathVariable Long userId) {
        List<AwardRecords> list = awardRecordsService.list(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<AwardRecords>()
                        .eq("user_id", userId)
        );
        return Result.ok(list);
    }
    //根据姓名查询
    @GetMapping("/name")
    public Result getByName(@RequestParam String name) {
        List<AwardRecords> list = awardRecordsService.list(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<AwardRecords>()
                        .like("name", name)
        );
        return Result.ok(list);
    }
}
